package com.test.demo;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;

/**
 * Created by jl on 2020/6/12
 * 用于查找下次比该数字大的数字的距离
 * eg： input-> [73,74,75,71,69,72,76,73]
 *     output-> [1,1,4,2,1,1,0,0]
 */
public class TemperatureStack {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        int[] input = Arrays.stream(StringUtils.split(line, ",")).mapToInt(Integer::parseInt).toArray();
        int[] output = findBiggerDistance(input);
        System.out.println(Arrays.toString(output));
    }

    private static int[] findBiggerDistance(int[] input) {
        int[] output = new int[input.length];
        Stack<DNode> stack = new Stack<>();
        for (int i = 0; i < input.length; i++) {
            while (!stack.empty() && input[i] > stack.peek().value) {
                output[stack.peek().index] = i - stack.peek().index;
                stack.pop();
            }
            stack.push(new DNode(i, input[i]));
        }
        while (!stack.empty()) {
            DNode node = stack.pop();
            output[node.index] = 0;
        }
        return output;
    }

    private static class DNode {
        int index;

        int value;

        DNode(int index, int value) {
            this.index = index;
            this.value = value;
        }
    }
}
